/**
 * WinMain.cpp		Copyright _ 2001 Li Zhaoming. All rights reserved.
 * Calls initialization functions and processes the message loop
 * PLATFORMS: Windows 95/98, Me, 2000
 */

#include "stdafx.h"
#include "resource.h"

// Global instance.
HINSTANCE g_hinstance;
HWND gFocus;
// String buffer.
char szAppName[20];		// The name of this application
char szTitle[50];		// The title bar text

/**
 * calls initialization function, processes message loop
 *
 * PARAMETERS:
 *		hInstance - The handle to the instance of this application that
 *		is currently being executed.
 *
 *		hPrevInstance - This parameter is always NULL in Win32
 *		applications.
 *
 *		lpCmdLine - A pointer to a null terminated string specifying the
 *		command line of the application.
 *
 *		nCmdShow - Specifies how the main window is to be diplayed.
 *
 * RETURN VALUE:
 *		If the function terminates before entering the message loop,
 *		return FALSE.
 *		Otherwise, return the WPARAM value sent by the WM_QUIT message.
 *
 * COMMENTS:
 *		Windows recognizes this function by name as the initial entry point
 *		for the program. This function calls the initialization routine.
 *		It then executes a message retrieval and dispatch loop that is the
 *		top-level control structure for the remainder of execution. The
 *		loop is terminated when a WM_QUIT message is received, at which
 *		time this function exits the application instance by returning the
 *		value passed by PostQuitMessage().
 *
 *		If this function must abort before entering the message loop, it
 *		returns the conventional value NULL.
 */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	HWND hwnd;
	HANDLE hMutex;
	MSG msg;
	HACCEL hAccelTable;

	// Load the application name and description strings.

	LoadString(hInstance, IDS_APPNAME, szAppName, sizeof(szAppName));
	LoadString(hInstance, IDS_TITLE, szTitle, sizeof(szTitle));

	// Save the instance handle in static variable, which will be used in
	// many subsequence calls from this application to Windows.

	g_hinstance = hInstance;	// Store instance handle in our global variable
	
	// Limit application to one instance.

	hMutex = CreateMutex (NULL, TRUE, szAppName);

	if (GetLastError () == ERROR_ALREADY_EXISTS)
	{
		if (hwnd = FindWindow(NULL, szTitle))
		{
			// Bring it to the top of Z order and active it.
			BringWindowToTop (hwnd);
			// Bring key input into this window.
			SetForegroundWindow (hwnd);
			// Display as normal window
			ShowWindow (hwnd, nCmdShow);
		}
		return 0;
	}

	// Create a main window for this application instance.

	hwnd = CreateDialog(hInstance, (LPCTSTR) IDD_APPLICATION, NULL, (DLGPROC) WndProc);

	hAccelTable = LoadAccelerators(hInstance, szAppName);

	// Acquire and dispatch messages until a WM_QUIT message is received.
	while (GetMessage(&msg, NULL, 0, 0))
	{
		HWND hwndx = GetForegroundWindow();
		if(IsWindow(hwndx))
		{
			if(hwndx != hwnd)
			{
				if(gFocus != hwndx)
				{
					if(IsWindow(gFocus))
					{
						AttachThreadInput(
							GetWindowThreadProcessId(hwnd,NULL),
							GetWindowThreadProcessId(gFocus,NULL),
							FALSE);
					}

					gFocus = hwndx;
					AttachThreadInput(
						GetWindowThreadProcessId(hwnd,NULL),
						GetWindowThreadProcessId(hwndx, NULL), 
						TRUE);
				}
			}
		}
		if (!hwnd || !IsDialogMessage(hwnd, &msg))
		{
			if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg))
			{
				TranslateMessage(&msg);	// Translates virtual key codes
				DispatchMessage(&msg);	// Dispatches message to window
			}
		}
	}

	// Release the mutex semaphore

	ReleaseMutex (hMutex);
	
	// Returns the value from PostQuitMessage
	
	return msg.wParam;

}
